package com.meepo.framework.design.text

import android.content.Context
import android.graphics.Color
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.TextPaint
import androidx.appcompat.widget.AppCompatTextView
import android.text.method.LinkMovementMethod
import android.text.style.AbsoluteSizeSpan
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import android.util.AttributeSet
import android.view.View

class SpannableTextView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
    AppCompatTextView(context, attrs, defStyleAttr) {


    companion object {
        const val ITEM_TYPE_TEXT = "text"

    }


    fun setSpans(vararg items: SpanItem) {
        this.text = create(items.asList())
    }

    /**
    Spannable. SPAN_INCLUSIVE_EXCLUSIVE 前面包括，后面不包括，意思就是在这段文本前部插入新的文本会应用该样式，而在文本后部插入新文本则不会应用该样式
    Spannable. SPAN_INCLUSIVE_INCLUSIVE 前面包括，后面包括，意思就是在这段文本前部插入新的文本会应用该样式，在文本后部插入新文本也会应用该样式
    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE 前面不包括，后面不包括
    Spanned.SPAN_EXCLUSIVE_INCLUSIVE 前面不包括，后面包括
     */

    private fun create(items: List<SpanItem>): SpannableStringBuilder {

        val builder = SpannableStringBuilder("")

        for (item in items) {


            val finalStr = SpannableString(item.value)

            val clickSpan = object : ClickableSpan() {
                override fun onClick(widget: View) {
                    item.onClick(item, widget)
                }

                override fun updateDrawState(ds: TextPaint) {
                    super.updateDrawState(ds)
                    ds.isUnderlineText = false
                    ds.clearShadowLayer()
                }
            }
            finalStr.setSpan(clickSpan, 0, item.value.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

            when (item.type) {
                "icon" -> {
//                        if (meta.value.length > 0) {
//                            var bm = BitmapHelper.parse(meta.value)
//                            val imageSpan = ImageSpan(instance, bm)
//                            finalStr.setSpan(imageSpan, 0, meta.value.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
//                        }
                }
                else -> {

                    var color = 0
                    if (item.color is Int) {
                        color = item.color
                    } else if (item.color is String) {
                        color = Color.parseColor(item.color)
                    }

                    if (color != 0) {
                        val colorSpan = ForegroundColorSpan(color)
                        finalStr.setSpan(colorSpan, 0, item.value.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
                    }


                    if (item.size > 0) {
                        finalStr.setSpan(AbsoluteSizeSpan(item.size), 0, item.value.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
                    }


                    if (item.typeface != 0) {
                        finalStr.setSpan(StyleSpan(item.typeface), 0, item.value.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
                    }
                }
            }

            builder.append(finalStr)
        }

        return builder
    }

    init {
        this.movementMethod = LinkMovementMethod.getInstance()
    }
}